package cn.iocoder.yudao.module.infra.controller.admin.label;

import cn.iocoder.yudao.module.infra.controller.admin.label.vo.*;
import cn.iocoder.yudao.module.infra.convert.label.LabelConvert;
import cn.iocoder.yudao.module.infra.dal.dataobject.label.LabelDO;
import cn.iocoder.yudao.module.infra.service.label.LabelService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 标签管理")
@RestController
@RequestMapping("/infra/label")
@Validated
public class LabelController {

    @Resource
    private LabelService labelService;

    @PostMapping("/create")
    @Operation(summary = "创建分类管理")
    @PreAuthorize("@ss.hasPermission('infra:label:create')")
    public CommonResult<Integer> createLabel(@Valid @RequestBody LabelCreateReqVO createReqVO) {
        return success(labelService.createLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分类管理")
    @PreAuthorize("@ss.hasPermission('infra:label:update')")
    public CommonResult<Boolean> updateLabel(@Valid @RequestBody LabelUpdateReqVO updateReqVO) {
        labelService.updateLabel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分类管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:label:delete')")
    public CommonResult<Boolean> deleteLabel(@RequestParam("id") Integer id) {
        labelService.deleteLabel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分类管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:label:query')")
    public CommonResult<LabelRespVO> getLabel(@RequestParam("id") Integer id) {
        LabelDO Label = labelService.getLabel(id);
        return success(LabelConvert.INSTANCE.convert(Label));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分类管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('infra:label:query')")
    public CommonResult<List<LabelRespVO>> getLabelList(@RequestParam("ids") Collection<Integer> ids) {
        List<LabelDO> list = labelService.getLabelList(ids);
        return success(LabelConvert.INSTANCE.convertIdsList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分类管理分页")
    @PreAuthorize("@ss.hasPermission('infra:label:query')")
    public CommonResult<PageResult<LabelRespVO>> getLabelPage(@Valid LabelPageReqVO pageVO) {
        PageResult<LabelDO> pageResult = labelService.getLabelPage(pageVO);
        return success(LabelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取分类精简信息列表", description = "只包含被开启的分类，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('infra:label:query')")
    public CommonResult<List<LabelBaseVO>> getSimpleLabelList() {
        List<LabelDO> list = labelService.getSimpleLabelList();
        // 排序后，返回给前端
        list.sort(Comparator.comparing(LabelDO::getSort));
        return success(LabelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/type")
    @Operation(summary = "获得标签分类列表")
    @PreAuthorize("@ss.hasPermission('infra:label:query')")
    public CommonResult<List<LabelBaseVO>> getType() {
        List<LabelDO> pageResult = labelService.getType();
        return success(LabelConvert.INSTANCE.convertList(pageResult));
    }

}
